﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendStar.Code;
using FriendStar.Domain.Entity.BaseData;
using FriendStar.Domain.Entity.MainData;
using FriendStar.Application.MainData;

namespace FriendStar.Web.Areas.MainData.Controllers
{
    public class StorageController : ControllerBase
    {
        // GET: /MainData/Storage/

        private CPK_ZD_11_DatStoreHouseApp service = new CPK_ZD_11_DatStoreHouseApp();
        private V_CPK_ZD_12_DatStoreHouseSetApp v_cpk_zd_12_datstorehousesetApp = new V_CPK_ZD_12_DatStoreHouseSetApp();
        private CPK_ZD_12_DatStoreHouseSetApp cpk_zd_12_datstorehousesetApp = new CPK_ZD_12_DatStoreHouseSetApp();

        #region 仓库定义
        public ActionResult StorageDefine()
        {
            return View();
        }
        public ActionResult DatStoreHouseList(string FStoreNO, string FStoreName, string FResPerson )
        {
            var data = service.GetStoreHouses("","",FStoreNO,FStoreName,FResPerson);
            return Content(data.ToJson());
        }
        /// <summary>
        /// 新增修改仓库定义
        /// </summary>
        /// <param name="itemsDetailEntity">仓库定义数据</param>
        /// <returns></returns>
        public ActionResult SubmitForm(CPK_ZD_11_DatStoreHouseEntity cpk_ZD_11_DatStoreHouseEntity)
        {
            if(string.IsNullOrEmpty(cpk_ZD_11_DatStoreHouseEntity.FStoreNO))
            {
                return Error("仓库编号不能为空!");
            }
            if(cpk_ZD_11_DatStoreHouseEntity.FStoreNO.Length>4)
            {
                return Error("仓库编号不能超过4位");
            }
            service.SubmitForm(cpk_ZD_11_DatStoreHouseEntity);
            return Success("操作成功。");
        }

        public ActionResult DeleteForm(string storeID)
        {
            service.DeleteForm(storeID);
            return Success("删除成功。");
        }

        #endregion 仓库定义

        #region 仓库货位存储策略

        #region 冲压件库货位存储策略
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult StrategyCP()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStrategyCPJson(Pagination pagination, string FStoreName, string FStoreSetNO)
        {
            var data = new
            {
                rows = v_cpk_zd_12_datstorehousesetApp.GetList(pagination, FStoreName, FStoreSetNO),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStrategyCPSortJson(string sidx, string sord, int rows, string FStoreName, string FStoreSetNO, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = v_cpk_zd_12_datstorehousesetApp.GetList(pagination, FStoreName, FStoreSetNO);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].FStoreSetID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitStrategyCPForm(string status, CPK_ZD_12_DatStoreHouseSetEntity entity, string keyValue)
        {
            if (status == "I")
            {
                if (cpk_zd_12_datstorehousesetApp.CheckedEntity(entity))
                {
                    cpk_zd_12_datstorehousesetApp.SubmitForm(entity, "");
                    return Success("新增操作成功。", entity.FStoreSetID);
                }
                else
                {
                    return Error("新增失败，已经存在相同货位编号数据。");
                }
            }
            else if (status == "U")
            {
                if (cpk_zd_12_datstorehousesetApp.CheckedEntity(entity))
                {
                    CPK_ZD_12_DatStoreHouseSetEntity oldEntity = cpk_zd_12_datstorehousesetApp.GetForm(entity.FStoreSetID);
                    if (oldEntity != null)
                    {
                        cpk_zd_12_datstorehousesetApp.SubmitForm(entity, entity.FStoreSetID);

                        return Success("修改操作成功。");
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的货位数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同货位编号数据。");
                }
            }
            else if (status == "D")
            {

                CPK_ZD_12_DatStoreHouseSetEntity oldEntity = cpk_zd_12_datstorehousesetApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    cpk_zd_12_datstorehousesetApp.DeleteForm(oldEntity.FStoreSetID);

                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的容器数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult StrategyCPInput(string status, string number)
        {
            ViewBag.status = (status != "U") ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.storeHouseList = service.GetSelectList();
            if (status == "U")
            {
                ViewBag.oldEntity = cpk_zd_12_datstorehousesetApp.GetForm(number).ToJson();
            }
            return View();
        }
        #endregion

        #endregion

        public ActionResult Strategy()
        {
            return View();
        }   

        public ActionResult Strategy2()
        {
            return View();
        }

        public ActionResult Strategy3()
        {
            return View();
        }

        public ActionResult Strategy4()
        {
            return View();
        }

        public ActionResult Strategy5()
        {
            return View();
        }

        public ActionResult StrategyInput()
        {
            return View();
        }

        public ActionResult StrategyInput2()
        {
            return View();
        }

        public ActionResult StrategyInput3()
        {
            return View();
        }
        public ActionResult StrategyInput4()
        {
            return View();
        }

        public ActionResult StrategyInput5()
        {
            return View();
        }

        public ActionResult ChoosePeople()
        {
            return View();
        }

        public ActionResult RecommendContainer()
        {
            return View();
        }

        public ActionResult RecommendContainer2()
        {
            return View();
        }
        public ActionResult RecommendContainer3()
        {
            return View();
        }
        public ActionResult RecommendContainer4()
        {
            return View();
        }

        public ActionResult RecommendStamping()
        {
            return View();
        }

        public ActionResult RecommendStamping3()
        {
            return View();
        }
        public ActionResult RecommendStamping4()
        {
            return View();
        }
        public ActionResult RecommendStamping5()
        {
            return View();
        }

        public ActionResult RecommendSupplier()
        {
            return View();
        }

        public ActionResult RecommendSupplier3()
        {
            return View();
        }
        public ActionResult RecommendSupplier4()
        {
            return View();
        }

        public ActionResult RecommendSupplier5()
        {
            return View();
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStrategyJson()
        {
            string parentId = Guid.NewGuid().ToString();
            List<TreeViewModel> treeList = new List<TreeViewModel>() { 
                new TreeViewModel(){id = parentId, text = "货位存储策略", value = "", parentId = "0", isexpand = true, complete = true, hasChildren = true},
                new TreeViewModel(){id = Guid.NewGuid().ToString(), text = "冲压件库货位存储策略", value = "StrategyCP", parentId = parentId, isexpand = true, complete = true, hasChildren = false},
                new TreeViewModel(){id = Guid.NewGuid().ToString(), text = "容器库货位存储策略", value = "StrategyRQ", parentId = parentId, isexpand = true, complete = true, hasChildren = false},
                new TreeViewModel(){id = Guid.NewGuid().ToString(), text = "维修备件库货位存储策略", value = "StrategyBJ", parentId = parentId, isexpand = true, complete = true, hasChildren = false},
                new TreeViewModel(){id = Guid.NewGuid().ToString(), text = "辅料库货位存储策略", value = "StrategyFL", parentId = parentId, isexpand = true, complete = true, hasChildren = false},               
                new TreeViewModel(){id = Guid.NewGuid().ToString(), text = "原材料库货位存储策略", value = "StrategyCL", parentId = parentId, isexpand = true, complete = true, hasChildren = false}
            };

            return Content(treeList.TreeViewJson());
        }

    }
}
